﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TRPOP.CMS.Model;
using TRPOP.CMS.Business;
using TRPOP.Common;
using System.Data;
using TRPOP.Config;
using TRPOP.CMS.Plugin;

namespace TRPOP.CMS.Web.ExtraBus
{
    /// <summary>
    /// 内容信息保存 内容信息获取 基本处理类
    /// </summary>
    public class Page_Content_Manage : BasePage
    {
        /// <summary>
        /// 保存信息响应的事件
        /// 可传递的参数有: ExtraUrl 外部URL
        ///                 HtmlName 发布文件名
        ///                 Name     标题
        ///                 SEODescription  SEOTitle SEOKey
        ///                 Template 模板文件
        ///                 TopWeight  EliteWeight  DisplayOrder
        ///                 SelfPSN SelfURL
        ///                 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static bool SaveInfo(int intContentId,int intNodeId,int intUserId)
        {
            bool blnReturn = false;

            ContentManage oContentManage = new ContentManage();
            ContentInfo oContentInfo = new ContentInfo();

            NodeInfo oNodeInfo = new NodeManage().GetInfoById(intNodeId);
            if (oNodeInfo == null)
                return blnReturn;

            // 初始化 Content
            #region
            if (intContentId > 0)
            {
                oContentInfo = (ContentInfo)oContentManage.GetInfoById(intContentId);
                oContentInfo.UpdateTime = DateTime.Now;
                oContentInfo.UpdateUserId = intUserId;
            }
            else
            {
                oContentInfo.NodeId = intNodeId;
                oContentInfo.AddTime = DateTime.Now;
                oContentInfo.AddUserId = intUserId;
                oContentInfo.PubTime = DateTime.Now;
                oContentInfo.PubStatus = 1;
                oContentInfo.UpdateTime = DateTime.Now;
                oContentInfo.UpdateUserId = intUserId;
                oContentInfo.Status = 1;
            }
            #endregion

            // 信息基表的信息处理
            #region 
            oContentInfo.ExtraUrl = RequestHelper.RequestString("ExtraUrl");
            oContentInfo.HtmlName = RequestHelper.RequestString("HtmlName");
            oContentInfo.Name = RequestHelper.RequestString("Name");
            oContentInfo.Type = (int)LinkType.EnumElement.RealLink;
            oContentInfo.PubUserId = intUserId;
            oContentInfo.ReadLimit = 0;
            oContentInfo.SEODescription = RequestHelper.RequestString("SEODescription");
            oContentInfo.SEOKey = RequestHelper.RequestString("SEOKey");
            oContentInfo.SEOTitle = RequestHelper.RequestString("SEOTitle");
            oContentInfo.Template = RequestHelper.RequestString("Template");
            oContentInfo.TopWeight = RequestHelper.RequestInt("TopWeight");
            oContentInfo.EliteWeight = RequestHelper.RequestInt("EliteWeight");
            oContentInfo.DisplayOrder = RequestHelper.RequestInt("DisplayOrder");
            oContentInfo.SelfPSN = RequestHelper.RequestString("SelfPSN");
            oContentInfo.SelfURL = RequestHelper.RequestString("SelfURL");
            if (RequestHelper.RequestString("status").IsEmpty()==false)
            {
                oContentInfo.Status = RequestHelper.RequestInt("Status");
            }
            #endregion

            // 信息扩展表的信息处理
            // 通过 TableName_FieldName 形式的key，获取 Request 页面传递过来的各个参数值
            #region 
            string strContentTitle = string.Empty;
            ModelInfo oModelInfo = (new ModelManage()).GetInfoById(oNodeInfo.ModelId);
            List<ModelFieldInfoWithTableName> oModelFieldInfoList = new List<ModelFieldInfoWithTableName>();
            foreach (ModelFieldInfoWithTableName FieldInfo in (new ModelFieldManage()).GetWithTableNameListByModelId(oNodeInfo.ModelId))
            {
                oModelFieldInfoList.Add(FieldInfo.Clone() as ModelFieldInfoWithTableName);
            }
            bool blnGetTableName = false;
            string strFormKey = string.Empty;
            foreach (ModelFieldInfoWithTableName FieldInfo in oModelFieldInfoList)
            {

                if (!blnGetTableName)
                {
                    // 把扩展表明 增加到基本信息里
                    oContentInfo.ExtendTableName = FieldInfo.TableName;
                    blnGetTableName = true;
                }

                strFormKey = FieldInfo.TableName + "_" + FieldInfo.FieldName.ToString();

                //不同的字段类型，不通的处理
                switch (FieldInfo.FieldTypeStr.ToLower())
                {
                    
                    case "varchar":
                    case "nvarchar":
                        FieldInfo.DefaultValue = StringExtension.CutStringWithByte(RequestHelper.RequestString(strFormKey), FieldInfo.FieldLength).SQLFilter().RemoveHtml();
                        break;
                    
                    case "bit":
                    case "int":
                        FieldInfo.DefaultValue = RequestHelper.RequestInt(strFormKey).ToString();
                        break;
                    
                    default:
                        FieldInfo.DefaultValue = RequestHelper.RequestString(strFormKey).SQLFilter();
                        break;

                }

                //标题字段的处理
                if (FieldInfo.IsTitleField)
                {
                    oContentInfo.Name = RequestHelper.RequestString(strFormKey).RemoveHtml();
                    strContentTitle = RequestHelper.RequestString(strFormKey).RemoveHtml();
                }
            }
            #endregion

            //保存信息
            EnumSubmitResult enumResult = oContentManage.SaveNewDoc(oContentInfo, oModelFieldInfoList);

            //如果保存页面成功，则更新相关的信息
            if (enumResult == EnumSubmitResult.Success)
            {
                //生成信息URL，并保存
                oContentInfo.Url = oContentManage.CreateURL(oNodeInfo, oContentInfo);
                blnReturn = true;
            }

            return blnReturn;
        }

        /// <summary>
        /// 获取内容列表
        /// </summary>
        /// <param name="intNodeID">结点ID</param>
        /// <param name="strReturnkey">返回的字段</param>
        /// <param name="strIgnore">排除的内容IDs</param>
        /// <param name="strOrderby"></param>
        /// <param name="strWhere"></param>
        /// <param name="blnSubNode"></param>
        /// <param name="blnIsPage"></param>
        /// <param name="intIndexPage">当前页次，从1开始</param>
        /// <param name="intPageSize"></param>
        /// <param name="IntRecount">总数量，分页时用到</param>
        /// <returns></returns>
        public static DataTable GetContentList(int intNodeID, string strReturnkey, string strIgnore, string strOrderby, string strWhere, bool blnSubNode, bool blnIsPage, int intIndexPage, int intPageSize, out int IntRecount)
        {
            IDAL m_IDAL = ITemplateAccess.GetIDalInstance();

            string strNum = string.Empty;

            if (blnIsPage)
            {
                int strStart = (intIndexPage-1) * intPageSize + 1;
                int strEnd = intIndexPage * intPageSize;
                strNum = strStart + "," + strEnd;
                IntRecount = m_IDAL.GetListByNodeids(intNodeID.ToString(), strWhere, strIgnore, false, blnSubNode);
            }
            else
                IntRecount = 0;

            return m_IDAL.GetListByNodeids(intNodeID.ToString(), strNum, strOrderby, strWhere, strIgnore, false, strReturnkey, blnSubNode);

        }
        /// <summary>
        /// 根据ID获取内容信息
        /// </summary>
        /// <param name="intID"></param>
        /// <returns></returns>
        public static DataRow GetContent(int intID)
        {
            IDAL m_IDAL = ITemplateAccess.GetIDalInstance();
            return m_IDAL.GetContent(intID.ToString(),false,false,string.Empty) as DataRow;
        }


    }
}
